﻿








#include "gzhu.h"
#include <QEventLoop>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtWidgets/QWidget>
#include <QTextCodec>
#include <QString>
#include <QUrl>
#include <QJsonParseError>
#include <QJsonObject>
#include <QRegExp>
#include <QRegularExpression>



gzhu::gzhu(database * Tempdb)
{

    db=Tempdb;
}




//广州大学
//找到下一页的网址
QString gzhu::search_next_page(QString data)
{

    QString i;

    QStringList linshi;

    i=QString("下页(.*?)class");

    linshi=search(i,data);

    if(linshi.size()>0)
    {

        linshi[0]=linshi[0].replace("下页</a><a href=\"","");

        linshi[0]=linshi[0].replace("\" class","");

        //qDebug()<<linshi[0];

        return linshi[0];

    }

    else

    {
        qDebug()<<"没有找到上下页";

        return "";

    }

}

//找到尾页的网址

QString gzhu::search_final_page(QString data)
{
/*
    QString i;

    QStringList linshi;

    i=QString("(.*)尾页(.*)");

    linshi=search(i,data);

    if(linshi.size()>0)
    {

        linshi[0]=linshi[0].replace(" <a href=\"","");

        linshi[0]=linshi[0].replace("\">尾页</a>\r","");

        // qDebug()<<linshi[0];

        return linshi[0];

    }

    else

    {*/
        qDebug()<<"没有找到尾页";

        return "";

    //}

}

//找到讲座网址
QStringList gzhu::search_url(QString data)
{

    QString i=QString("(.*)学术报告");

    QStringList linshi;

    linshi=search(i,data);

    //移除非网址部分

    i=QString("(.*)<a href=\"..");

    QString i2=QString("\" target(.*)");

    QStringList useless_part;

    for(int a=0;a<linshi.length();a++)

    {

        //qDebug()<<linshi[a];

        useless_part = search(i,linshi[a]);

        linshi[a].replace(useless_part[0],"");

        useless_part = search(i2,linshi[a]);

        linshi[a].replace(useless_part[0],"");

    }
    qDebug()<<linshi;

    return linshi;

}

//找到通知发布日期
QStringList gzhu::search_date(QString data)
{
    QString i=QString("time(.*)");


    QStringList linshi;

    linshi=search(i,data);

    //移除非日期部分

    i=QString("</span>(.*)");

    QString i2=QString("(.*)time\">");

    QStringList useless_part;

    for(int a=0;a<linshi.length();a++)

    {

        //qDebug()<<linshi[a];

        useless_part = search(i,linshi[a]);

        linshi[a].replace(useless_part[0],"");

        useless_part = search(i2,linshi[a]);

        linshi[a].replace(useless_part[0],"");

    }
    //qDebug()<<"广大 date"<<linshi;
    return linshi;
}

//找到通知页面的讲座信息
QStringList gzhu::search_data(QString data)
{

    //抓取论文信息

    QStringList title,lecturer,time,place,university,url;

    QStringList useless_part;

    useless_part<<"&nbsp;"<<"<(.*?)>";

    QStringList temp;

    //处理信息

    if(data.size()>0)
    {
        for(int b=0;b<useless_part.size();b++)
        {
            temp = search(useless_part[b],data);
            if(temp.size()>0)
                for(int c=0;c<temp.size();c++)
                    data.replace(temp[c],"");
        }
    }
    else {
        qDebug()<<"通知网页数据错误";
        return temp;
    }

    //查找标题

    QString regulardata="题(.*)目(.*)";

    QStringList searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {
        //删去无效信息
        for(int a=0;a<searchdata.size();a++)
        {
            temp = search("题目(.*): ",searchdata[a]);
            searchdata[a].replace(temp[0],"");

            title.append(searchdata[a]);
        }

    }


            else
            {
                qDebug()<<"查找标题时发生错误";

                title.append("出错\r");
            }






    qDebug()<<title;


    //获取报告人

    regulardata="报告人:(.*)";

    searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {
        for(int a=0;a<searchdata.size();a++)
        {
            searchdata[a].replace("报告人:","");

            lecturer.append(searchdata[a]);
        }
    }


        else
        {

            qDebug()<<"查找报告人失败";

            lecturer.append("出错\r");
        }



    qDebug()<<lecturer;

    //查找时间

    regulardata="时间:(.*)";

    searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {

        for(int a=0;a<searchdata.size();a++)
        {
            searchdata[a].replace("时间: ","");

            searchdata[a] = date_form(searchdata[a]);
            time.append(searchdata[a]);
        }

    }

    else

    {

        qDebug()<<"查找时间失败";

        time.append("出错\r");

    }

    qDebug()<<time;

    //查找地点

    regulardata="地点: (.*)";

    searchdata=search(regulardata,data);

    if(searchdata.size()>0)

    {
        for(int a=0;a<searchdata.size();a++)
        {
            searchdata[a].replace("地点: ","");

            place.append(searchdata[a]);
        }

    }

    else

    {

        qDebug()<<"查找地址失败";

        place.append("出错\r");

    }

    qDebug()<<place;


    QStringList senddata;

    if(title[0]=="出错")
    {

    }


    for(int a=0;a<title.length();a++)
    {
        senddata.append(title[a]);

        senddata.append(lecturer[a<lecturer.size()?a:lecturer.size()-1]);

        senddata.append(time[a<time.size()?a:time.size()-1]);

        senddata.append(place[a<place.size()?a:place.size()-1]);

    }


    //qDebug()<<senddata;

    return senddata;

}


bool gzhu::dealing()
{

    //下面获取暨南大学学术报告信息

    //获取当前年份
    current_date =QDate::currentDate();

    //ui->setupUi(this);



    //找出最后更新的讲座

    QSqlQuery query(db->getDataBase());


    query.exec("SELECT link FROM information_list where college='广州大学'");

    query.next();//这里指向的是第一条信息

    last_lecture=query.value(0).toString();


    starturl="http://jsj.gzhu.edu.cn";

    weburl="/xsky1/xshdtz.htm";

    indexpage=get(starturl+weburl);

    if(indexpage.size()==0)
        return true;


    nexturl=search_next_page(indexpage);

    //循环所有页面
    do{
        //获取讲座网页的url
        lecture_list = search_url(indexpage);

        lecture_date_list = search_date(indexpage);

        nexturl=search_next_page(indexpage);

        //读取每个网页的信息
        for(int a=0;a<lecture_list.length();a++)
        {

            lecture_date = QDate::fromString(lecture_date_list[a],"yyyy-MM-dd");

            qDebug()<<lecture_list[a]<<lecture_date;

            qDebug()<<"MY_DEBUG :"<<last_lecture<<" and "<<lecture_list[a];

            //如果与之前更新的讲座重复，跳出
            if(last_lecture.contains(lecture_list[a]))
            {

                qDebug()<<"广州大学更新完成";
                nexturl="/xsky1/xshdtz/1.htm";
                break;
            }

            //如果时间超过一年,则跳出
            if(lecture_date.daysTo(current_date)>500)
            {
                qDebug()<<"超过一年";
                nexturl="/xsky1/xshdtz/1.htm";
                break;
            }

            lecture_data = search_data(get(starturl+lecture_list[a]));


            //将信息存入数据库
            for(int i=0;i<lecture_data.length();i=i+4)
            {
                //lecture_data的类型相当于一个数组，从中按顺序获取各个属性
                QString title,lecture,time,place,url;
                title=lecture_data[i];
                lecture=lecture_data[i+1];
                time=lecture_data[i+2];
                place=lecture_data[i+3];
                url=starturl+lecture_list[a];
                db->insert_notice(lecture_date_list[a],url);
                db->insert_report(title,lecture,time,place,"广州大学",url);

            }
        }

        indexpage=get(starturl+nexturl);

    }while(nexturl!="/xsky1/xshdtz/1.htm");

    //file.close();
    return true;

}
